<template>
  <div class="student-container">
    <el-card>
      <template #header>
        <div class="clearfix">
          <span>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>用户管理</el-breadcrumb-item>
              <el-breadcrumb-item>学生管理</el-breadcrumb-item>
            </el-breadcrumb></span
          >
        </div>
      </template>
      <div>
        <el-button
          type="primary"
          size="small"
          @click="
            drawerIsShow = true;
            (editStudentId = ''), (eventType = 'add');
          "
          >创建学生</el-button
        >
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="keyword"
          size="small"
          style="width: 250px; margin-left: 15px"
        >
        </el-input>
      </div>
      <el-table
        :data="students"
        v-loading="loading"
        :height="maxHeight"
        :stripe="true"
        style="width: 100%; top: 15px"
      >
        <!-- <el-table-column type="selection" width="55" fixed="left">
        </el-table-column> -->

        <el-table-column label="学号" width="120" fixed="left">
          <template #default="scope"
            ><el-link
              type="primary"
              @click="editStudent(scope.row.studentNum)"
              href="javascript:;"
              >{{ scope.row.studentNum }}</el-link
            ></template
          >
        </el-table-column>

        <el-table-column
          prop="studentName"
          width="80"
          label="姓名"
          fixed="left"
        >
        </el-table-column>
        <el-table-column label="头像" width="80"
          ><template #default="scope">
            <el-avatar
              v-if="scope.row.picture"
              size="small"
              :src="scope.row.picture"
            ></el-avatar>
            <div v-else>无</div>
          </template>
        </el-table-column>
        <el-table-column label="性别" width="80"
          ><template #default="scope">
            <el-tag v-if="scope.row.studentGender === '男'" type="warning"
              >男
            </el-tag>
            <el-tag v-else-if="scope.row.studentGender === '女'" type="info"
              >女
            </el-tag>
            <el-tag v-else effect="plain">保密</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="collageName" label="学院" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="majorName" label="专业" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="className" label="班级" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="studentCardId"
          label="身份证号"
          show-overflow-tooltip
          width="200"
        ></el-table-column>

        <el-table-column
          prop="address"
          label="通讯地址"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="最近更新"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="username" label="用户名" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="电话号码" width="120" fixed="right">
          <template #default="scope">
            <el-link
              type="primary"
              @click="opendialog(scope.row.telephone)"
              href="javascript:;"
              >{{ scope.row.telephone }}</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top: 30px">
        <el-pagination
          @size-change="sizeChange"
          @current-change="pageChange"
          :current-page="page"
          :page-sizes="[15, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :disabled="loading"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
  <studentDrawer
    :isShow="drawerIsShow"
    :drawerClose="drawerClose"
    :eventType="eventType"
    :editStudentId="editStudentId"
  />
  <div class="account-dialog">
    <el-dialog
      :title="`账户 ${account.telephone} 详情`"
      v-model="dialogVisible"
      width="30%"
    >
      <el-card class="box-card" shadow="never">
        <div>用户名称：{{ account.username }}</div>
        <!-- <div>
          用户角色：<el-tag type="success">{{ account.userTypeValue }}</el-tag>
        </div>
        <div>
          能否添加子账户：<el-tag type="danger">{{
            account.addpowerValue
          }}</el-tag>
        </div> -->
        <div>创建时间：{{ account.createTime }}</div>
        <div>最近更新：{{ account.updateTime }}</div>
      </el-card>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="danger" @click="unbind(account.telephone)"
            >解除绑定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { computed, reactive, toRefs, watch } from "vue";
import { UserService, Account } from "@/api/user";
import { useStore } from "vuex";
import studentDrawer from "./studentDrawer.vue";
import Code from "@/consts/common";

export default {
  name: "StudentList",
  components: { studentDrawer },
  setup() {
    const store = useStore();

    let data = reactive({
      keyword: "",
      eventType: "add",
      drawerIsShow: false,
      dialogVisible: false,
      editStudentId: "",
      account: new Account(),
      students: computed(() => store.state.Student.students),
      loading: computed(() => store.state.Student.loading),
      total: computed(() => store.state.Student.total),
      page: computed(() => store.state.Student.page),
      size: computed(() => store.state.Student.pageSize),
      maxHeight: document.documentElement.clientHeight - 280,
      drawerClose: () => {
        data.drawerIsShow = false;
      },
      editStudent: (studentNum: string) => {
        data.editStudentId = studentNum;
        data.eventType = "edit";
        data.drawerIsShow = true;
      },
      pageChange: (page: number) => {
        store.dispatch("Student/pageChange", {
          page: page,
          size: data.size,
          keyword: data.keyword,
        });
      },
      sizeChange: (size: number) => {
        store.dispatch("Student/pageSizeChange", {
          page: data.page,
          size: size,
          keyword: data.keyword,
        });
      },
      opendialog: (telephone: string) => {
        UserService.userList({ page: 1, keyword: telephone }).then((res) => {
          if (res.code == Code.CODE_SUCCESS) {
            res.data.data.map((item) => {
              if (item.telephone == telephone) {
                data.account = item;
              }
            });
          }
        });
        data.dialogVisible = true;
      },
      unbind: (telephone: string) => {
        data.dialogVisible = false;
        store.dispatch("Student/unbing", {
          telephone,
          page: data.page,
          size: data.size,
          keyword: data.keyword,
        });
        data.account = new Account();
      },
    });

    const listStudent = (
      page: number = 1,
      size: number = 15,
      keyword: string = ""
    ) => {
      store.dispatch("Student/listStudent", { page, size, keyword });
    };
    listStudent();

    watch(
      () => data.keyword,
      (newValue, oldVlaue) => {
        store.dispatch("Student/pageChange", 1);
        listStudent(1, data.size, newValue);
      }
    );
    const getHeight = () => {
      data.maxHeight = document.documentElement.clientHeight - 280;
    };

    window.addEventListener("resize", getHeight, false);
    return {
      ...toRefs(data),
    };
  },
};
</script>

<style lang="less" scoped>
.box-card {
  .el-card__body > div {
    margin: 15px 0px;
  }
}
</style>
<style lang="less">
.account-dialog {
  .el-card__body {
    padding: 5px 15px !important;
  }
  .el-dialog__body {
    padding: 20px 20px;
  }
}
</style>